{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 导入库\n",
    "import numpy as np\n",
    "import time\n",
    "import random"
   ],
   "id": "4569a537b4811651",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy inf和nan",
   "id": "6f82d0dde724d81"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# inf表示无穷大，nan表示非数字:缺少数值\n",
    "# 类型为float\n",
    "\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))\n",
    "print(b, type(b))"
   ],
   "id": "67d3ecd5346ecdcd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# np.nan!= np.nan 结果为True\n",
    "print(np.nan != np.nan)\n",
    "\n",
    "# nan和其他数运算，结果为nan\n",
    "np.nan + 1"
   ],
   "id": "5d3987493183fa72",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:01:34.389298Z",
     "start_time": "2025-01-08T06:01:34.383983Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 判断数组中nan的个数\n",
    "#np.isnan(a): 判断数组中是否有nan元素,返回布尔值数组.有nan元素返回True,否则返回False.\n",
    "#np.count_nonzero()：统计数组中非零元素的个数.True为1,False为0.\n",
    "\n",
    "# 创建一个数组\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "\n",
    "# 将三行四列的数改成nan\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "print(t)\n",
    "\n",
    "# 统计nan的个数\n",
    "# np.count_nonzero(np.isnan(t)): 判断数组中非零元素的个数\n",
    "print(np.isnan(t))\n",
    "print(np.count_nonzero(t != t))"
   ],
   "id": "287b8b8767481a34",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 处理nan",
   "id": "d5f39caab2a0c9dd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:01:25.624507Z",
     "start_time": "2025-01-08T06:01:25.619578Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)\n"
   ],
   "id": "a4cc93e7cd6e6e31",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15.  0. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(4, 6)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:15:27.588973Z",
     "start_time": "2025-01-08T06:15:27.584994Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print(t.shape)\n",
    "\n",
    "# 遍历每一列，判断这一列是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "    # 获取当前列数据\n",
    "    temp_col = t[:, i]\n",
    "\n",
    "    # 判断是否有nan\n",
    "    if np.count_nonzero(np.isnan(temp_col)) != 0:\n",
    "        # 将不为nan的元素拿出来，计算均值\n",
    "        temp_col_not_non = temp_col[temp_col == temp_col]\n",
    "        print(temp_col_not_non)\n",
    "        # 将nan替换为均值\n",
    "        temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_non)\n",
    "\n",
    "print(t)"
   ],
   "id": "41195cb3df2d5756",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15.  4. 17.]\n",
      " [18. 19. 20. 21.  4. 23.]]\n",
      "(4, 6)\n",
      "[ 3. 15. 21.]\n",
      "[4. 4. 4.]\n",
      "[ 5. 17. 23.]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13.  4. 15.]\n",
      " [12. 13. 14. 15.  4. 17.]\n",
      " [18. 19. 20. 21.  4. 23.]]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:18:13.365126Z",
     "start_time": "2025-01-08T06:18:13.362126Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.inf = np.inf\n",
    "# -np.inf = -np.inf\n",
    "# np.nan != np.nan\n",
    "# np.nan +np.inf = np.nan\n",
    "print(np.inf == np.inf)\n",
    "print(np.nan == np.nan)\n",
    "print(np.inf + np.nan == np.nan)"
   ],
   "id": "4ee1e17324e3502d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 转置与轴滚动",
   "id": "3f88f34774c1016b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:23:13.904305Z",
     "start_time": "2025-01-08T06:23:13.900304Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3, 4)\n",
    "print('原数组：')\n",
    "print(a)\n",
    "\n",
    "#np.transpose(a): 转置数组.\n",
    "# 原数组的行变成列，列变成行.\n",
    "print('对换数组：')\n",
    "print(np.transpose(a))\n",
    "\n",
    "# a.T: 轴滚动.\n",
    "# 原数组的行变成列，列变成行.\n",
    "print('转置数组：')\n",
    "print(a.T)"
   ],
   "id": "8570f80dc4e234d4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:24:51.328700Z",
     "start_time": "2025-01-08T06:24:51.324876Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# .swapaxes(axis1, axis2): 交换两个轴.\n",
    "#  原数组的axis1轴变成axis2轴，axis2轴变成axis1轴.\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "re1 = t1.swapaxes(1, 0)\n",
    "\n",
    "print(' 原 数 组 ：')\n",
    "print(t1)\n",
    "print(t1.shape)\n",
    "print('调用 swapaxes 函数后的数组：')\n",
    "print(re1)\n",
    "print(re1.shape)"
   ],
   "id": "5534fd9366cc00ff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n",
      "(6, 4)\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:25:44.722260Z",
     "start_time": "2025-01-08T06:25:44.717866Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#如果是3维及以上，称为轴交换\n",
    "t3 = np.arange(60).reshape(3, 4, 5)\n",
    "print(' 原 数 组 ：')\n",
    "print(t3.shape)\n",
    "\n",
    "t3 = np.swapaxes(t3, 1, 2)\n",
    "print('调用 swapaxes 函数后的数组：')\n",
    "print(t3.shape)"
   ],
   "id": "2f6ba7a3d77b1fdd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "(3, 4, 5)\n",
      "调用 swapaxes 函数后的数组：\n",
      "(3, 5, 4)\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:27:04.293138Z",
     "start_time": "2025-01-08T06:27:04.289811Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# swapaxes每次只能交换两个轴\n",
    "# rollaxis: 类似于转置，但是可以指定轴的位置.\n",
    "# 多维多个轴可以同时交换.\n",
    "\n",
    "# np.ones: 创建一个全1数组.\n",
    "a = np.ones((3, 4, 5, 6))\n",
    "\n",
    "# 滚动轴3到1的位置\n",
    "b = np.rollaxis(a, 3, 1)\n",
    "print(b.shape)\n",
    "\n",
    "#轴会滚动，直到它位于此位置之前\n",
    "c = np.rollaxis(b, 1, 4)\n",
    "print(c.shape)"
   ],
   "id": "bacd2912c87359ed",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "84e16c17f455ffa5"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
